package com.book;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.slayer.model.BMSBook;
import com.slayer.service.BMSBookLocalServiceUtil;

import java.io.IOException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
 * Portlet implementation class BookPortlet
 */
public class BookPortlet extends MVCPortlet {
			public void updateBook(ActionRequest actionRequest, 
					ActionResponse actionResponse) 
							throws IOException, PortletException{
				String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
				String author = ParamUtil.getString(actionRequest, "author");
		
				long bookId = ParamUtil.getLong(actionRequest, "bookId");
				if(bookId > 01){
				BMSBookLocalServiceUtil.modifyBook(bookId, bookTitle, author);
				} else {
					BMSBookLocalServiceUtil.insertBook(bookTitle, author);
				}
				
				/*actionResponse.setRenderParameter("jspPage",BookConstants.PAGE_SUCCESS);*/ 
				
				actionResponse.setRenderParameter("jspPage", (bookId > 01) ? BookConstants.SHOW_BOOK : BookConstants.PAGE_SUCCESS);
				
			}
			
			public void deleteBook(ActionRequest actionRequest,
					ActionResponse actionResponse)
			throws IOException, PortletException{
				
				long bookId = ParamUtil.getLong(actionRequest, "bookId");
				if (bookId > 01){
					try {
						BMSBookLocalServiceUtil.deleteBMSBook(bookId);
					} catch (PortalException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SystemException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");
				actionResponse.sendRedirect(redirectURL);
			}
			
		    public void render(RenderRequest request, RenderResponse response)
					throws PortletException, IOException{
				setSortParams(request);
				super.render(request, response);
			}

            private void setSortParams(RenderRequest request) {
				String jspPage = ParamUtil.getString(request, "jspPage");
				
				if (jspPage.equalsIgnoreCase(BookConstants.SHOW_BOOK)){
					
					String orderByCol = ParamUtil.getString(request, "orderByCol", "bookTitle");
					request.setAttribute("orderByCol", orderByCol);
					
					String orderByType = ParamUtil.getString(request, "orderByType", "asc");
					request.setAttribute("orderByType", orderByType);
				}
				
			}
            
            public void deleteBooks(ActionRequest actionRequest, 
            		ActionResponse actionResponse) 
            				throws IOException, PortletException {
            	
            	String bookIdsForDelete = ParamUtil.getString(actionRequest, "bookIdsForDelete");
            	
            	//convert this JSON format.
            	bookIdsForDelete = "["+ bookIdsForDelete +"]";
            	
            	/*the presence of ":" in the string create problems while prasing.
            	replace all the occurance of ":" withsome other unique string, eg."-"*/
            	
            	bookIdsForDelete = bookIdsForDelete.replaceAll(":", "-");
            	//parse and get a JSON array of objects
            	
            	JSONArray jsonArray = null;
            	
            	try {
					jsonArray = JSONFactoryUtil.createJSONArray(bookIdsForDelete);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	
            	//process the jsonArray
            	
            	if (Validator.isNotNull(jsonArray)) {
            		for (int i=0; i<jsonArray.length(); i++){
            			JSONObject jsonObject = jsonArray.getJSONObject(i);
            			
            			long bookId = jsonObject.getLong("bookId");
            			try {
							BMSBookLocalServiceUtil.deleteBMSBook(bookId);
						} catch (PortalException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (SystemException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
            		}
            	}
            	//redirect to the list page again.
            	actionResponse.setRenderParameter("jspPage", BookConstants.SHOW_BOOK);
				
			}
            
            public void searchBooks(ActionRequest actionRequest, 
            		ActionResponse actionResponse) 
            				throws IOException, PortletException{
            	String searchTerm = ParamUtil.getString(actionRequest, "searchTerm");
            	
            	if (Validator.isNull(searchTerm)) return;
            	
            		try{
            			List<BMSBook> bmsBooks = BMSBookLocalServiceUtil.searchBooks(searchTerm);
            			
            			actionRequest.setAttribute("SEARCH_RESULT", bmsBooks);
            			actionResponse.setRenderParameter("jspPage", BookConstants.SHOW_BOOK);
            		} catch (SystemException e) {
            			e.printStackTrace();
            		}
            }
}
